/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controlador;

import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import modelo.Usuarios;

@WebFilter(filterName = "Filtro", urlPatterns = {"/*"})
public class Filtro implements Filter {

    public Filtro() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        HttpSession session = httpRequest.getSession();
        Usuarios usu = (Usuarios) session.getAttribute("USUARIO");
        StringBuffer url = httpRequest.getRequestURL();
        String url1 = "http://localhost:8080"+httpRequest.getContextPath()+"/".toString();
        String url2 = "http://localhost:8080"+httpRequest.getContextPath()+"/login.jsp".toString();
        String url3 = "http://localhost:8080"+httpRequest.getContextPath()+"/IniServlet".toString();
        String url4 = "http://localhost:8080"+httpRequest.getContextPath()+"/css/style.css".toString();
        String url0 = url.toString();
        if (url0.toString().equals(url1) && usu == null || url0.toString().equals(url2) && usu == null || url0.toString().equals(url3) && usu == null|| url0.toString().equals(url4) && usu == null) {
            chain.doFilter(request, response);
        } else {
            boolean condi = ServletHelper.validarUsu(httpRequest, httpResponse);

            if (condi) {
                String url100 = "http://localhost:8080"+httpRequest.getContextPath()+"/".toString();
                String url200 = "http://localhost:8080"+httpRequest.getContextPath()+"/login.jsp".toString();
                StringBuffer ur = httpRequest.getRequestURL();
                if (ur.toString().equals(url100) || ur.toString().equals(url200)) {
                    httpResponse.sendRedirect("/TT2/index.jsp");
                } else {
                    
                    StringBuilder buffer = new StringBuilder();
                    for (Enumeration parametrosPeticion = request.getParameterNames();
                            parametrosPeticion.hasMoreElements();) {
                        String nombreParametro = (String) parametrosPeticion.nextElement();
                        String valoresDeParametros[] = request.getParameterValues(nombreParametro);
                        int numeroParametros = valoresDeParametros.length;
                        buffer.append(nombreParametro);
                        buffer.append(" = ");
                        for (int i = 0; i < numeroParametros; i++) {
                            buffer.append(valoresDeParametros[i]);
                            buffer.append(" | ");
                            if (i < numeroParametros - 1) {
                                buffer.append(",");
                            }
                        }
                    }
                    System.out.println("Recibida peticion Desde la IP: "
                            + request.getRemoteAddr());
                    if (!buffer.toString().equals("")) {
                        System.out.println("\tla peticion tiene los parametros: " + buffer);
                    }
                }

                chain.doFilter(request, response);
            }
        }
    }

    @Override
    public void destroy() {
    }

    @Override
    public void init(FilterConfig filterConfig) {
    }
}
